﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace Wechat.TenpayV3
{
    /// <summary>
    /// 支付交易数据
    /// </summary>
    public class TransactionData
    {
        readonly JObject jo;
        public TransactionData(string json)
        {
            jo = JObject.Parse(json);
        }

        public TransactionData(JObject data)
        {
            jo = data;
        }

        /*
         * {
            "sp_appid":"wx8888888888888888",
            "sp_mchid":"1230000109",
            "sub_appid":"wxd678efh567hg6999",
            "sub_mchid":"1900000109",
            "out_trade_no":"1217752501201407033233368018",
            "trade_state_desc":"支付成功",
            "trade_type":"MICROPAY",
            "attach":"自定义数据",
            "transaction_id":"1217752501201407033233368018",
            "trade_state":"SUCCESS",
            "bank_type":"CMC",
            "success_time":"2018-06-08T10:34:56+08:00",
            "amount":{
                "payer_total":100,
                "total":100,
                "currency":"CNY",
                "payer_currency":"CNY"
            },
            "promotion_detail":[...],
            "payer":{
                "openid":"oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"
            },
            "scene_info":{...}
        }
         */

        /// <summary>
        /// 交易状态，枚举值（SUCCESS：支付成功，REFUND：转入退款，NOTPAY：未支付，CLOSED：已关闭，REVOKED：已撤销（付款码支付），USERPAYING：用户支付中（付款码支付），PAYERROR：支付失败(其他原因，如银行返回失败)
        /// </summary>
        public string TradeState { get { return jo["trade_state"].ToString(); } }

        /// <summary>
        /// 商户订单号（这里在订单ID前面加了一个前缀字母用于区分交易的商品类型）
        /// </summary>
        public string OutTradeNo { get { return jo["out_trade_no"].ToString(); } }

        /// <summary>
        /// 微信支付系统生成的订单号。 示例值：1217752501201407033233368018
        /// </summary>
        public string TransactionID { get { return jo["transaction_id"].ToString(); } }

        /// <summary>
        /// 交易类型，枚举值（JSAPI：公众号支付，NATIVE：扫码支付，APP：APP支付，MICROPAY：付款码支付，MWEB：H5支付，FACEPAY：刷脸支付
        /// </summary>
        public string TradeType { get { return jo["trade_type"].ToString(); } }

        /// <summary>
        /// 银行类型，采用字符串类型的银行标识。银行标识请参考《银行类型对照表》 示例值：CMC
        /// </summary>
        public string BankType { get { return jo["bank_type"].ToString(); } }

        /// <summary>
        /// 附加数据
        /// </summary>
        public string Attach { get { return jo["attach"].ToString(); } }

        /// <summary>
        /// 支付成功时间
        /// </summary>
        public DateTime SuccessTime { get { return DateTime.Parse(jo["success_time"].ToString()); } }

        /// <summary>
        /// 用户支付金额，单位为分。
        /// </summary>
        public int PayerTotal { get { return int.Parse(jo["amount"]["payer_total"].ToString()); } }

        /// <summary>
        /// 订单总金额，单位为分。
        /// </summary>
        public int Total { get { return int.Parse(jo["amount"]["total"].ToString()); } }
    }
}
